package com.example.system.controller;

import com.example.system.common.Result;
import com.example.system.entity.Remind;
import com.example.system.exception.CustomException;
import com.example.system.service.impl.RemindServiceImpl;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/remind")
@MapperScan("com.*.mapper")
public class RemindController {
    @Autowired
    private RemindServiceImpl remindService;

    //增删改查
    //新增 & 修改
    @PostMapping("/add")
    public Result addRemindByRemind(@RequestBody Remind remind) {
        if (remind.getRemindId() == null) {
            try {
                remindService.addRemindByRemind(remind);
            } catch (CustomException e) {
                return Result.error(e.getMsg());
            }
        } else {
            try {
                remindService.updateRemindByRemind(remind);
            } catch (CustomException e) {
                return Result.error(e.getMsg());
            }
        }
        return Result.success();
    }

    // 根据提醒ID删除提醒信息
    @DeleteMapping("/delete/{remindId}")
    public Result deleteRemindByRemindId(@PathVariable Integer remindId) {
        Integer deleteRes = remindService.deleteRemindByRemindId(remindId);
        return Result.success(deleteRes);
    }


    // 根据提醒ID查提醒信息
    @GetMapping("/search/self/{remindId}")
    public Result findRemindByRemindID(@PathVariable Integer remindId) {
        Remind curRemind = remindService.findRemindByRemindId(remindId);
        if (curRemind == null) {
            return Result.error("当前用户不存在");
        }
        return Result.success(curRemind);
    }

}
